package com.tofindaway.jms;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import org.springframework.jms.core.support.JmsGatewaySupport;

public class JmsReceiver extends JmsGatewaySupport {

	public void receiverTextMsg() {
		JFrame jr = new JFrame();
		JPanel jp = new JPanel();
		final JTextArea text = new JTextArea(200, 200);
		JButton jb = new JButton("Clear");
		jp.add(text);
		jp.add(jb);
		jp.setLayout(new GridLayout(2, 1));
		jr.add(jp);
		jr.setVisible(true);
		jr.setSize(400, 400);
		jb.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				text.setText("");
			}
		});
		while (true) {
			ObjectMessage message = (ObjectMessage) this.getJmsTemplate().receive();

			try {
				MyMsg msg = (MyMsg) message.getObject();
				text.setText(text.getText() + " " + msg.getTitle() + "\t" + message.getStringProperty("time") + "\n  " + msg.getContent() + "\n");
				System.out.println("get value：" + text.getText());
			} catch (JMSException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}